﻿using Leeder.Logger;
using SPSAndong.Business;
using SPSAndong.Comm;
using SPSAndong.Handle;
using SPSAndong.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SPSAndong.Forms
{
    public partial class FrmDeviceMonitor : UIPage
    {
        /// <summary>
        /// 日志
        /// </summary>
        public LogOperate LogObj => Log.GetLogger(this.GetType());
        bool isLoad = true;      //判断窗体是否首次加载
        SPSAndongHandler sPSAndongHandler;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        public FrmDeviceMonitor()
        {
            InitializeComponent();
        }

        private void FrmDeviceMonitor_Load(object sender, EventArgs e)
        {
            //如果窗体已经加载过则不重新加载
            if (!isLoad)
                return;
            isLoad = false;
            asc.controllInitializeSize(this.uiTabControl1);
            sPSAndongHandler = SPSAndongHandler.getInstance();
            sPSAndongHandler.OnDeviceStateChanged += OnDeviceStateChanged_Event;
        }

        /// <summary>
        /// 设备状态改变触发
        /// </summary>
        private void OnDeviceStateChanged_Event()
        {
            UpdateUI_DeviceCommunication();
            UpdateUI_IoCommunication();
        }

        /// <summary>
        /// 主要设备通讯监控UI刷新
        /// </summary>
        private void UpdateUI_DeviceCommunication()
        {
            if (this.panelLip.InvokeRequired)
            {
                try
                {
                    this.Invoke(new MethodInvoker(() =>
                    {
                        UpdateUI();
                    }));
                }
                catch (Exception ex)
                {
                    LogObj.Error(ex.Message);
                }
            }
            else
            {
                UpdateUI();
            }
        }

        /// <summary>
        /// 远程I/O通讯状态UI刷新
        /// </summary>
        private void UpdateUI_IoCommunication()
        {
            foreach (var model in GolbalVariable.SystemDeviceBag.Where(x => x.DataSource == "PLC"))
            {
                UpdateIoStationUI(model);
            }
        }

        private void UpdateUI()
        {
            if (GolbalVariable.LipState)
                this.panelLip.FillColor = Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(208)))), ((int)(((byte)(80)))));
            else
                this.panelLip.FillColor = Color.Red;
            if (GolbalVariable.PlcState)
                this.panelPlc.FillColor = Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(208)))), ((int)(((byte)(80)))));
            else
                this.panelPlc.FillColor = Color.Red;
        }

        /// <summary>
        /// 更新IO箱状态
        /// </summary>
        /// <param name="systemDevice">设备对象</param>
        private void UpdateIoStationUI(SystemDeviceModel systemDevice)
        {
            var control = this.tabPage2.Controls.Find(systemDevice.DeviceName, true);
            if (control != null && control.Length > 0)
            {
                UIPanel panel = control[0] as UIPanel;
                if (systemDevice.Status == 1)
                    panel.FillColor = Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(208)))), ((int)(((byte)(80)))));
                else
                    panel.FillColor = Color.Red;
            }
        }

        /// <summary>
        /// 更新PLC电源状态
        /// </summary>
        /// <param name="systemDevice">设备对象</param>
        private void UpdatePlcPowerUI(SystemDeviceModel systemDevice)
        {
            var control = this.tabPage3.Controls.Find(systemDevice.DeviceName, true);
            if (control != null && control.Length > 0)
            {
                UIPanel panel = control[0] as UIPanel;
                if (systemDevice.Status == 1)
                    panel.FillColor = Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(208)))), ((int)(((byte)(80)))));
                else
                    panel.FillColor = Color.Red;
            }
        }

        private void FrmDeviceMonitor_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this.uiTabControl1);
        }

        /// <summary>
        /// 绑定UPS状态数据
        /// </summary>
        /// <param name="model"></param>
        //private void BindData(UpsModel model)
        //{
        //    //PLC电源
        //    if (GolbalVariable.SystemDeviceBag.Where(x => x.DeviceName == "PLC电源" && x.DataSource == "OPC").FirstOrDefault().Status == 0)
        //    {
        //        this.btnplcpower.BackColor = Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(208)))), ((int)(((byte)(80)))));
        //        this.btnplcpower.Text = "电源电压正常";
        //    }
        //    else
        //    {
        //        this.btnplcpower.BackColor = Color.Red;
        //        this.btnplcpower.Text = "电源电压异常";
        //    }
        //    //UPS电源
        //    if (model.State != null && model.QueryResult)
        //    {
        //        if (model.State.UpsFault == 0)
        //        {
        //            if (model.State.BatteryVoltage == 1)
        //            {
        //                this.btnUPSPower.BackColor = Color.Red;
        //                this.btnUPSPower.Text = model.State.BatteryVoltageText;
        //            }
        //            else
        //            {
        //                this.btnUPSPower.BackColor = Color.FromArgb(((int)(((byte)(146)))), ((int)(((byte)(208)))), ((int)(((byte)(80)))));
        //                this.btnUPSPower.Text = model.State.BatteryVoltageText;
        //            }
        //        }
        //        else
        //        {
        //            this.btnUPSPower.BackColor = Color.Red;
        //            this.btnUPSPower.Text = model.State.UpsFaultText;
        //        }
        //    }
        //    else
        //    {
        //        this.btnUPSPower.BackColor = Color.Red;
        //        this.btnUPSPower.Text = model.ErrorMsg;
        //    }
        //}

    }
}
